const URL_HEAD = "http://localhost:8080/";

function onUserManageLoad() {
    findAllUser();
}
function onCheckToken(){
    const uuid = $("#txtUuid").val();
    $.ajax({
        url:URL_HEAD + "token/success",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"uuid": uuid},
        dataType: "json",
        success: function (result){
            alert("UUID " + uuid + " 存在");
        },
        error: function (){
            showError("UUID " + uuid + " 不存在", );
            window.location.href = 'URL_HEAD' + 'login';
        }
    });
}
function  onLogon(){
    const userName = $("#txtInsertUserName").val();
    const passWord = $("#txtInsertPassWord").val();
    const email = $("#txtInsertEmail").val();
    $.ajax({
        url:URL_HEAD + "user/logon",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"name": userName , "password": passWord, "email": email},
        dataType: "json",
        success: function (result){
            alert("注册成功");
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户注册异常", xhr, textStatus, errorThrown);
        }
    });
}
function  onLogin(){
    const userName = $("#txtLoginUserName").val();
    const passWord = $("#txtLoginPassWord").val();
    $.ajax({
        url:URL_HEAD + "user/login",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"name": userName , "password": passWord},
        dataType: "json",
        success: function (result){
            //alert("登录成功");
            alert('URL_HEAD' + 'success');
            window.location.href = 'URL_HEAD' + 'success';
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户登录异常", xhr, textStatus, errorThrown);
        }
    });
}
function  onAddUser(){
    const userName = $("#txtInsertUserName").val();
    const email = $("#txtInsertEmail").val();
    const passWord = $("#txtInsertPassWord").val();
    const age = Number($("#txtInsertAge").val());
    $.ajax({
        url:URL_HEAD + "user/add",
        //通过type来判断调用哪个方法
        type: "post",
        data: {"name": userName , "password": passWord, "email": email, "age": age},
        dataType: "json",
        success: function (result){
            alert(result.msg);
            findAllUser();
        },
        error: function (xhr, textStatus, errorThrown){
            showError("用户注册异常", xhr, textStatus, errorThrown);
        }
    });
}
function onFindUser(){
    const userName = $("#txtFindUserName").val();
    const passWord = $("#txtInsertPassWord").val();

    $.ajax({
        url: URL_HEAD + "user/login",
        //通过type来判断调用哪个方法
        type: "get",
        data: {"name": userName, "password": passWord},
        dataType: "json",
        success: function (result){
            clearUserList();
            fillUserList(result.data);
        },
        error: function (xhr, textStatus, errorThrown){
            showError("查询用户异常", xhr, textStatus, errorThrown);
        }
    });
}

function findAllUser() {
    findUser(0, null, null, 0);
}
function fillUserList(users) {
    let builder = new StringBuilder();
    for(let i = 0; i < users.length; i++){
        let user = users[i];
        builder.clear();
        builder.append("<tr><td>");
        builder.append(user.id);
        builder.append("</td><td>");
        builder.append(user.name);
        builder.append("</td><td>");
        builder.append(user.password);
        builder.append("</td><td>");
        builder.append(user.email);
        builder.append("</td><td>");
        builder.append(user.age);
        builder.append("</td></tr>");
        $("#tbUserList tbody").append(builder.toString());
    }
}
function clearUserList(){
    $("tbUserList tbody").empty();
}
function onUpdateUser() {
    const id = Number($("#txtUpdateId").val());
    const userName = $("#txtInsertUserName").val();
    const password = $("#txtInsertPassword").val();
    const email = $("#txtInsertEmail").val();
    const age = Number($("#txtInsertAge").val());
    if(id && !(userName || password || email || age)){
        alert("请保证更新参数不为空");
        return;
    }
    $.ajax({
        url: URL_HEAD + "user/update",
        //通过type来判断调用哪个方法
        type: "put",
        data: {"id": id , "name": userName, "email": email, "age": age},
        dataType: "json",
        success: function (result){
            alert(result.msg);
            findAllUser();
        },
        error: function (xhr, textStatus, errorThrown){
            showError("更新用户信息异常", xhr, textStatus, errorThrown);
        }
    });
}
function onDeleteUser() {
    const id = Number($("#txtDeleteId").val());
    const userName = $("#txtDeleteUserName").val();
    const email = $("#txtDeleteEmail").val();
    const age = Number($("#txtDeleteAge").val());
    $.ajax({
        url: URL_HEAD + "user/delete",
        //通过type来判断调用哪个方法
        type: "delete",
        data: {"id": id , "name": userName, "email": email, "age": age},
        dataType: "json",
        success: function (result){
            alert(result.msg);
            findAllUser();
        },
        error: function (xhr, textStatus, errorThrown){
            showError("删除用户信息异常", xhr, textStatus, errorThrown);
        }
    });
}
